package com.cs;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @ClassName: NIOFileChannel03
 * @Description: TODO
 * @Author: kevinHan
 * @Date: 2020/8/6 21:26
 * @Version: 1.0
 */
public class NIOFileChannel03 {

    public static void main(String[] args) throws Exception {
        File file = new File("C:\\Users\\kevinHan\\IdeaProjects\\802\\my_netty\\NIO_demo\\1.txt");
        FileInputStream fileInputStream = new FileInputStream(file);

        FileChannel fileChannel01 = fileInputStream.getChannel();

        FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\kevinHan\\IdeaProjects\\802\\my_netty\\NIO_demo\\2.txt");
        FileChannel fileChannel02 = fileOutputStream.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(100);

        // 循环读取
        while (true) {
            int read = fileChannel01.read(byteBuffer);
            System.out.println("read = " + read);
            if (read != -1) {
                byteBuffer.flip();
                fileChannel02.write(byteBuffer);
                byteBuffer.clear();
            }
            else {
                break;
            }
        }
    }
}
